<template>
  <div class="login-container">
    <div class="login-box">
      <div class="avatar-box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <div class="input-box">
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item prop="username">
            <el-input
              prefix-icon="el-icon-user"
              v-model="form.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              prefix-icon="el-icon-lock"
              v-model="form.password"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="checkForm">登录</el-button>
            <el-button type="info" @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      // 登录表单得数据绑定对象
      form: {
        username: 'admin',
        password: '123456'
      },
      // 表单得验证规则
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在6到16个字符', trigger: 'blur' }
        ]
      },
      dialogVisible: false
    }
  },
  methods: {
    resetForm() {
      // 重置表单数据
      this.$refs.form.resetFields()
    },
    checkForm() {
      // 验证表单所有数据
      this.$refs.form.validate(async (valid) => {
        if (!valid) return false
        const { data: res } = await this.$http.post('login', {
          username: this.form.username,
          password: this.form.password
        })

        if (res.meta.status !== 200) {
          return this.$message.error('用户名或密码错误！')
        }
        this.$message.success('登陆成功')
        // 登陆成功后把服务器发来的token存到sessionStorage中，
        // 项目中出了登陆页面，必须在登录之后才能访问
        sessionStorage.setItem('token', res.data.token)
        sessionStorage.setItem('hash', '/home/users')
        // 通过编程式导航跳转到后台主页，路由地址是 /home
        this.$router.push({
          name: 'home'
        })
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #2b4b6b;

  .login-box {
    position: relative;
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    box-shadow: 0 0 10px #ddd;

    .avatar-box {
      position: absolute;
      left: 50%;
      top: -65px;
      transform: translateX(-50%);
      height: 130px;
      width: 130px;
      border-radius: 50%;
      border: 5px solid #eee;
      overflow: hidden;
      background-color: #fff;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .input-box {
      width: 100%;
      position: absolute;
      bottom: 0;
      padding: 0 20px;
      .el-form-item {
        label {
          text-align: center;
        }
      }
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
